package com.SinaMicroblogging.UI;

import java.io.IOException;
import java.io.InputStream;

import com.Util.HttpUtility;
import com.Util.String2;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class BlockView extends View {
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		invalidate();
		return true;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}

	private String title;
	private Bitmap img;
	private String fullText;
	private Paint mPaint;
	private int blockHeight;
	private Rect rect;

	public BlockView(Context context) {
		super(context);
		init();
	}

	public BlockView(Context context, AttributeSet attributeSet, int ds) {
		super(context, attributeSet, ds);
		init();
	}

	public BlockView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		init();
	}

	private void init() {
		blockHeight = 50;
		this.setClickable(true);
		this.setFocusable(true);
		mPaint = new Paint();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		if (this.hasFocus() || this.isPressed()) {
			mPaint.setColor(Color.GRAY);
		} else {
			mPaint.setColor(Color.WHITE);
		}
		int rectTop = getPaddingTop();
		int rectLeft = getPaddingLeft();
		int rectRight = getWidth() - getPaddingRight();
		int rectBottom = getPaddingBottom();
		rect = new Rect(rectLeft, rectTop, rectRight, rectTop + blockHeight);
		canvas.drawRect(rect, mPaint);
		mPaint.setColor(Color.GRAY);
		canvas.drawLine(rectLeft, rectTop, rectRight, rectTop, mPaint);
		canvas.drawLine(rectLeft, rectTop + blockHeight, rectRight, rectTop
				+ blockHeight, mPaint);
		drawImg(canvas, rectLeft, rectTop);
		drawTile(canvas, rectLeft, rectTop);
		drawFulltext(canvas, rectLeft, rectTop);
	}

	private void drawTile(Canvas canvas, int rectLeft, int rectTop) {
		if (!String2.isNullOrEmpty(title)) {
			int textLeft = img.getWidth() + rectLeft;
			Typeface typeface = Typeface.create("Arial", Typeface.BOLD_ITALIC);
			int ascent = (int) mPaint.ascent();
			mPaint.setTypeface(typeface);
			mPaint.setTextSize(10);
			mPaint.setColor(Color.BLACK);
			canvas.drawText(title, textLeft, rectTop - ascent + 5, mPaint);

		}

	}

	private void drawFulltext(Canvas canvas, int rectLeft, int rectTop) {
		if (!String2.isNullOrEmpty(fullText)) {
			int textLeft = img.getWidth() + rectLeft;
			int ascent = (int) mPaint.ascent();
			Typeface typeface = Typeface.create("Arial", Typeface.NORMAL);
			mPaint.setTypeface(typeface);
			mPaint.setTextSize(10);
			mPaint.setColor(Color.BLACK);
			canvas.drawText(fullText, textLeft, rectTop - ascent + 15, mPaint);
		}

	}

	private void drawImg(Canvas canvas, int rectLeft, int rectTop) {
		if (img != null) {
			canvas.drawBitmap(img, rectLeft, rectTop, mPaint);
		}

	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Bitmap getImg() {
		return img;
	}

	public void setImg(Bitmap img) {
		this.img = img;
	}

	public String getFullText() {
		return fullText;
	}

	public void setFullText(String fullText) {
		this.fullText = fullText;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int measuredHight = measureHight(heightMeasureSpec);
		int measuredWidth = measureWidth(widthMeasureSpec);

		this.setMeasuredDimension(measuredWidth, measuredHight);
	}

	private int measureHight(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int desiredHeight = blockHeight + getPaddingTop() + getPaddingBottom();
		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			return specSize;
		} else if (specMode == MeasureSpec.AT_MOST) {
			return desiredHeight < specSize ? desiredHeight : specSize;
		} else {
			return desiredHeight;
		}

	}

	private int measureWidth(int measureSpec) {

		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		int desiredWidth = 300 + getPaddingLeft() + getPaddingRight();
		if (specMode == MeasureSpec.EXACTLY) {
			// We were told how big to be
			return specSize;
		} else if (specMode == MeasureSpec.AT_MOST) {
			return desiredWidth < specSize ? desiredWidth : specSize;
		} else {
			return desiredWidth;
		}
	}

	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(focused, direction, previouslyFocusedRect);
		invalidate();
	}

}